package net.cloudcodex.client.character.presenter;

import net.cloudcodex.client.CloudCodexEventBus;
import net.cloudcodex.client.cdo.CharacterCDO;
import net.cloudcodex.client.character.view.CharacterDescriptionPopupView;
import net.cloudcodex.client.character.view.CharacterDescriptionPopupViewImpl;
import net.cloudcodex.client.character.view.CharactersView.Message;
import net.cloudcodex.client.util.rpc.SuccessCallback;
import net.cloudcodex.shared.dto.Result;
import net.cloudcodex.shared.dto.campaign.character.CharacterDescriptionDTO;
import net.cloudcodex.shared.dto.campaign.character.CharacterHeaderDTO;
import net.cloudcodex.shared.dto.campaign.character.CompleteCharacterDescriptionDTO;
import net.cloudcodex.shared.dto.home.CampaignSummaryDTO;
import net.cloudcodex.shared.rpc.CampaignRPCAsync;

import com.google.inject.Inject;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;

/**
 * 
 * 
 * @author ADaroussin
 */
@Presenter(view = CharacterDescriptionPopupViewImpl.class)
public class CharacterDescriptionPopupPresenter extends
		BasePresenter<CharacterDescriptionPopupView, CloudCodexEventBus> {
	private final Message message;
	private String nickName;
	private CampaignRPCAsync campaignService;
	private long userId;

	@Inject
	public CharacterDescriptionPopupPresenter(Message pMessage,
			CampaignRPCAsync campaignService) {
		this.message = pMessage;
		this.campaignService = campaignService;
	}

	@Override
	public void bind() {

	}

	public void showCharacterDescriptionPopup(long campaignId,
			final long loggedCharacterId, final long selectedCharacterId) {
		displayPopup();
	}

	public void onShowCharacterDescriptionPopup(long campaignId,
			final CharacterHeaderDTO ownerSelectedCharacterHeaderDTO,
			final CharacterHeaderDTO clickedCharacterHeaderDTO) {

		campaignService.getCharacterDescription(campaignId,
				clickedCharacterHeaderDTO.getId(),
				ownerSelectedCharacterHeaderDTO.getId(), null,
				new SuccessCallback<Result<CharacterDescriptionDTO>>() {

					@Override
					public void onSuccess(Result<CharacterDescriptionDTO> result) {
						CharacterDescriptionDTO completeCharacterDescriptionDTO = result
								.getData();
						CharacterCDO characterCDO = new CharacterCDO(
								clickedCharacterHeaderDTO,
								completeCharacterDescriptionDTO);
						CharacterDescriptionPresenter characterDescriptionPresenter = eventBus
								.addHandler(CharacterDescriptionPresenter.class);
						characterDescriptionPresenter
								.setByCharacterId(ownerSelectedCharacterHeaderDTO
										.getId());
						characterDescriptionPresenter
								.initCharacterDescriptionPopup(characterCDO,
										isOwner(characterCDO.getOwnerId()),
										false, null);
					}
				});

	}

	private void displayPopup() {
		this.view.showPopup();
	}

	public void onSetNickName(String nickName) {
		this.nickName = nickName;
	}

	public void onSetUserId(long userId) {
		this.userId = userId;
	}

	protected boolean isOwner(Long ownerId) {
		if (ownerId != null)
			return userId == ownerId;
		return false;
	}

	protected boolean isGameMaster(CampaignSummaryDTO campaignSummaryDTO) {
		return nickName.equals(campaignSummaryDTO.getGameMasterNickname());
	}
}
